#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <math.h>
#include <time.h>

#include <unistd.h>
#include <sys/wait.h>


#include "rdb_lib.h"
#include "CalDefine.h"
#include "CalExtern.h"
#include "CalSubDef.h"

int	GetCurLowPlanVal();
int	GetCurUpPlanVal();

// =====================================================================
int	ZeroCopyPlanProcess()
{
	if(NewDay != 1)
		return(0);
	CopyTomToToday();
	Copy2DayToTom();
	Copy3DayTo2Day();
	Copy4DayTo3Day();
	Copy5DayTo4Day();
	Copy6DayTo5Day();
	Copy7DayTo6Day();
	Copy8DayTo7Day();
	return(0);
}


//LIUJI 2000.6.16
// =====================================================================
/*int	CopyTomToToday()
{
	int		RetCode, BufSize, TodayBufsize;
	int		loop, RecNum;
	char*		BufPtr, *TodayBufPtr;
	plan_tomorrow_scada*	PlanTomRecPtr;
	plan_today_scada*	PlanTodayRecPtr;


	BufSize = 8;
	BufPtr = (char* )malloc(BufSize);
	RecNum = GetAllRec(SCADA, PLAN_TOMORROW_NO, BufPtr, BufSize);
	if(RecNum <= 0)  {
		free(BufPtr);
		return(-1);
	}

	PlanTomRecPtr = (plan_tomorrow_scada* )BufPtr;

	TodayBufsize = (RecNum+1) * sizeof(plan_today_scada);
	TodayBufPtr = (char* )malloc(TodayBufsize);

	PlanTodayRecPtr = (plan_today_scada* )TodayBufPtr;
	for(loop=0; loop<RecNum; loop++) {
		
		memcpy((char* )&(PlanTodayRecPtr[loop].val[0]), 
		       (char* )&(PlanTomRecPtr[loop].val[0]), 100*sizeof(float));
		strcpy(PlanTodayRecPtr[loop].time_dot, PlanTomRecPtr[loop].time_dot);
		PlanTodayRecPtr[loop].id = PlanTomRecPtr[loop].id - 1000000;
		
	}

	for(loop = 0; loop< 100; loop++)
		PlanTodayRecPtr[289].val[loop] = PlanTomRecPtr[0].val[loop];

	PutAllRec(SCADA,  PLAN_TODAY_NO, (char* )TodayBufPtr, TodayBufsize);
	free(BufPtr);
	free(TodayBufPtr);
	return(0);

}*/


int	CopyTomToToday()
{
	int		RetCode, BufSize;
	int		loop, RecNum;
	char*		BufPtr;
	plan_today_scada*	PlanPtr;

	BufSize = 8;
	BufPtr = (char* )malloc(BufSize);
	RecNum = GetAllRec(SCADA, PLAN_TOMORROW_NO, BufPtr, BufSize);
	//if(RecNum <= 0)  {
	if (RecNum < 290) {
		printf("plan_tomorrow 's record_num is less than 290 error ! %d \n", RecNum);
		free(BufPtr);
		return(-1);
	}

	PlanPtr = (plan_today_scada* )BufPtr;
	for(loop=0; loop<RecNum; loop++)
		PlanPtr[loop].id = PLAN_TODAY_NO * 1000000 + loop + 1;
	
	for(loop = 0; loop< 100; loop++)
		PlanPtr[289].val[loop] = PlanPtr[0].val[loop];
		//PlanPtr[1441].val[loop] = PlanPtr[0].val[loop];
	PutAllRec(SCADA,  PLAN_TODAY_NO, BufPtr, BufSize);
	free(BufPtr);

	return(0);

}
// ====================================================================

int	Copy2DayToTom()
{
	int		RetCode, BufSize;
	int		loop, RecNum;
	char*		BufPtr;
	plan_tomorrow_scada*	PlanPtr;

	BufSize = 8;
	BufPtr = (char* )malloc(BufSize);
	
	RecNum = GetAllRec(SCADA, PLAN_2DAYS_LATER_NO, BufPtr, BufSize);
	//if(RecNum <= 0)  {
	if (RecNum < 290) {
		printf("plan_2days_later 's record_num is less than 290 error ! %d \n", RecNum);
		free(BufPtr);
		return(-1);
	}

	PlanPtr = (plan_tomorrow_scada* )BufPtr;
	for(loop=0; loop<RecNum; loop++)  
		PlanPtr[loop].id = PLAN_TOMORROW_NO * 1000000 + loop + 1;
	
	for(loop = 0; loop< 100; loop++)
		PlanPtr[289].val[loop] = PlanPtr[0].val[loop];
		//PlanPtr[1441].val[loop] = PlanPtr[0].val[loop];
	PutAllRec(SCADA,  PLAN_TOMORROW_NO, BufPtr, BufSize);

	free(BufPtr);

	return(0);

}


int	Copy3DayTo2Day()
{
	int		RetCode, BufSize;
	int		loop, RecNum;
	char*		BufPtr;
	plan_2days_later_scada*	PlanPtr;

	BufSize = 8;
	BufPtr = (char* )malloc(BufSize);
	RecNum = GetAllRec(SCADA, PLAN_3DAYS_LATER_NO, BufPtr, BufSize);
	//if(RecNum <= 0)  {
	if(RecNum < 290)  {
		printf("plan_3days_later 's record_num is wrong error ! %d \n", RecNum);
		free(BufPtr);
		return(-1);
	}

	PlanPtr = (plan_2days_later_scada* )BufPtr;
	for(loop=0; loop<RecNum; loop++)  {
		PlanPtr[loop].id = PLAN_2DAYS_LATER_NO * 1000000 + loop + 1;
	}
	for(loop = 0; loop< 100; loop++)
		PlanPtr[289].val[loop] = PlanPtr[0].val[loop];
		//PlanPtr[1441].val[loop] = PlanPtr[0].val[loop];
	PutAllRec(SCADA,  PLAN_2DAYS_LATER_NO, BufPtr, BufSize);
	free(BufPtr);

	return(0);
}


int	Copy4DayTo3Day()
{
	int		RetCode, BufSize;
	int		loop, RecNum;
	char*		BufPtr;
	plan_3days_later_scada*	PlanPtr;

	BufSize = 8;
	BufPtr = (char* )malloc(BufSize);
	RecNum = GetAllRec(SCADA, PLAN_4DAYS_LATER_NO, BufPtr, BufSize);
	//if(RecNum <= 0)  {
	if(RecNum < 290)  {
		printf("plan_4days_later 's record_num is wrong error ! %d \n", RecNum);
		free(BufPtr);
		return(-1);
	}

	PlanPtr = (plan_3days_later_scada* )BufPtr;
	for(loop=0; loop<RecNum; loop++)  {
		PlanPtr[loop].id = PLAN_3DAYS_LATER_NO * 1000000 + loop + 1;
	}
	for(loop = 0; loop< 100; loop++)
		PlanPtr[289].val[loop] = PlanPtr[0].val[loop];
		//PlanPtr[1441].val[loop] = PlanPtr[0].val[loop];
	PutAllRec(SCADA,  PLAN_3DAYS_LATER_NO, BufPtr, BufSize);
	free(BufPtr);

	return(0);
}

int	Copy5DayTo4Day()
{
	int		RetCode, BufSize;
	int		loop, RecNum;
	char*		BufPtr;
	plan_4days_later_scada*	PlanPtr;

	BufSize = 8;
	BufPtr = (char* )malloc(BufSize);
	RecNum = GetAllRec(SCADA, PLAN_5DAYS_LATER_NO, BufPtr, BufSize);
	//if(RecNum <= 0)  {
	if(RecNum < 290)  {
		printf("plan_5days_later 's record_num is wrong error ! %d \n", RecNum);
		free(BufPtr);
		return(-1);
	}

	PlanPtr = (plan_4days_later_scada* )BufPtr;
	for(loop=0; loop<RecNum; loop++)  {
		PlanPtr[loop].id = PLAN_4DAYS_LATER_NO * 1000000 + loop + 1;
	}
	for(loop = 0; loop< 100; loop++)
		PlanPtr[289].val[loop] = PlanPtr[0].val[loop];
		//PlanPtr[1441].val[loop] = PlanPtr[0].val[loop];
	PutAllRec(SCADA,  PLAN_4DAYS_LATER_NO, BufPtr, BufSize);
	free(BufPtr);

	return(0);
}

int	Copy6DayTo5Day()
{
	int		RetCode, BufSize;
	int		loop, RecNum;
	char*		BufPtr;
	plan_5days_later_scada*	PlanPtr;

	BufSize = 8;
	BufPtr = (char* )malloc(BufSize);
	RecNum = GetAllRec(SCADA, PLAN_6DAYS_LATER_NO, BufPtr, BufSize);
	//if(RecNum <= 0)  {
	if(RecNum < 290)  {
		printf("plan_6days_later 's record_num is wrong error ! %d \n", RecNum);
		free(BufPtr);
		return(-1);
	}

	PlanPtr = (plan_5days_later_scada* )BufPtr;
	for(loop=0; loop<RecNum; loop++)  {
		PlanPtr[loop].id = PLAN_5DAYS_LATER_NO * 1000000 + loop + 1;
	}
	for(loop = 0; loop< 100; loop++)
		PlanPtr[289].val[loop] = PlanPtr[0].val[loop];
		//PlanPtr[1441].val[loop] = PlanPtr[0].val[loop];
	PutAllRec(SCADA,  PLAN_5DAYS_LATER_NO, BufPtr, BufSize);
	free(BufPtr);

	return(0);
}

int     Copy7DayTo6Day()
{
        int             RetCode, BufSize;
        int             loop, RecNum;
        char*           BufPtr;
        plan_6days_later_scada* PlanPtr;

        BufSize = 8;
        BufPtr = (char* )malloc(BufSize);
        RecNum = GetAllRec(SCADA, PLAN_7DAYS_LATER_NO, BufPtr, BufSize);
        //if(RecNum <= 0)  {
        if(RecNum < 290)  {
                printf("plan_7days_later 's record_num is wrong error ! %d \n", RecNum);
                free(BufPtr);
                return(-1);
        }

        PlanPtr = (plan_6days_later_scada* )BufPtr;
        for(loop=0; loop<RecNum; loop++)  {
                PlanPtr[loop].id = PLAN_6DAYS_LATER_NO * 1000000 + loop + 1;
        }
        for(loop = 0; loop< 100; loop++)
                PlanPtr[289].val[loop] = PlanPtr[0].val[loop];
                //PlanPtr[1441].val[loop] = PlanPtr[0].val[loop];
        PutAllRec(SCADA,  PLAN_6DAYS_LATER_NO, BufPtr, BufSize);
        free(BufPtr);

        return(0);
}


int     Copy8DayTo7Day()
{
        int             RetCode, BufSize;
        int             loop, RecNum;
        char*           BufPtr;
        plan_7days_later_scada* PlanPtr;

        BufSize = 8;
        BufPtr = (char* )malloc(BufSize);
        RecNum = GetAllRec(SCADA, PLAN_8DAYS_LATER_NO, BufPtr, BufSize);
        //if(RecNum <= 0)  {
        if(RecNum < 290)  {
                printf("plan_8days_later 's record_num is wrong error ! %d \n", RecNum);
                free(BufPtr);
                return(-1);
        }

        PlanPtr = (plan_7days_later_scada* )BufPtr;
        for(loop=0; loop<RecNum; loop++)  {
                PlanPtr[loop].id = PLAN_7DAYS_LATER_NO * 1000000 + loop + 1;
        }
        for(loop = 0; loop< 100; loop++)
                PlanPtr[289].val[loop] = PlanPtr[0].val[loop];
                //PlanPtr[1441].val[loop] = PlanPtr[0].val[loop];
        PutAllRec(SCADA,  PLAN_7DAYS_LATER_NO, BufPtr, BufSize);
        free(BufPtr);

        return(0);
}


int	GetCurPlanVal()
{
	static int ccc = 0;
	ccc ++;
	if(ccc < ONCE_1_MIN)
		return(0);
	ccc = 0;
	
	int		RetCode, BufSize, RecNum;
	int		loop, SampleFrontNum, SampleCurNum;
	char*		BufPtr;
	float		TempVal;
	plan_today_scada*	PlanTodayRecPtr;

	BufSize = 8;
	BufPtr = (char* )malloc(BufSize);
	RecNum = GetAllRec(SCADA, PLAN_TODAY_NO, BufPtr, BufSize);
	if(RecNum < 290)  {
		free(BufPtr);
		return(-1);
	}

	SampleFrontNum = ((CurrentTime % 86400) / ONCE_5_MIN);
	PlanTodayRecPtr = (plan_today_scada* )BufPtr;

	for(loop = 0; loop < 100; loop++)  
		PlanTodayRecPtr[289].val[loop] = PlanTodayRecPtr[SampleFrontNum].val[loop];
		//PlanTodayRecPtr[1441].val[loop] = PlanTodayRecPtr[SampleFrontNum].val[loop];

	PutAllRec(SCADA,  PLAN_TODAY_NO, BufPtr, BufSize);
        free(BufPtr);

	GetCurLowPlanVal();
	GetCurUpPlanVal();
	return(0);
}

// =====================================================================
int     GetCurLowPlanVal()
{
        int             RetCode, BufSize, RecNum;
        int             loop, SampleFrontNum, SampleCurNum;
        char*           BufPtr;
        float           TempVal;
        plan_low_limit_scada*       PlanTodayRecPtr;

        if((CurrentTime % (ONCE_5_MIN)) != 0)
                return(0);

        BufSize = 8;
        BufPtr = (char* )malloc(BufSize);
        RecNum = GetAllRec(SCADA, PLAN_LOW_LIMIT_NO, BufPtr, BufSize);
        if(RecNum < 290)  {
                free(BufPtr);
                return(-1);
        }

        SampleFrontNum = ((CurrentTime % 86400) / ONCE_5_MIN);
        PlanTodayRecPtr = (plan_low_limit_scada* )BufPtr;

        for(loop = 0; loop < 100; loop++)
                PlanTodayRecPtr[289].val[loop] = PlanTodayRecPtr[SampleFrontNum].val[loop];
                //PlanTodayRecPtr[1441].val[loop] = PlanTodayRecPtr[SampleFrontNum].val[loop];

        PutAllRec(SCADA,  PLAN_LOW_LIMIT_NO, BufPtr, BufSize);
        free(BufPtr);
        return(0);
}
// =====================================================================
int     GetCurUpPlanVal()
{
        int             RetCode, BufSize, RecNum;
        int             loop, SampleFrontNum, SampleCurNum;
        char*           BufPtr;
        float           TempVal;
        plan_up_limit_scada*       PlanTodayRecPtr;

        if((CurrentTime % (ONCE_5_MIN)) != 0)
                return(0);

        BufSize = 8;
        BufPtr = (char* )malloc(BufSize);
        RecNum = GetAllRec(SCADA, PLAN_UP_LIMIT_NO, BufPtr, BufSize);
        if(RecNum < 290)  {
                free(BufPtr);
                return(-1);
        }

        SampleFrontNum = ((CurrentTime % 86400) / ONCE_5_MIN);
        PlanTodayRecPtr = (plan_up_limit_scada* )BufPtr;

        for(loop = 0; loop < 100; loop++)
                PlanTodayRecPtr[289].val[loop] = PlanTodayRecPtr[SampleFrontNum].val[loop];
                //PlanTodayRecPtr[1441].val[loop] = PlanTodayRecPtr[SampleFrontNum].val[loop];
	
        PutAllRec(SCADA,  PLAN_UP_LIMIT_NO, BufPtr, BufSize);
        free(BufPtr);
        return(0);
}
// =====================================================================
/****
int	PlanSybaseSample()
{
	
#define	SAMPLE_FIELD_NUM	50

	int		RetCode, BufSize;
	int		loop, loop2, RecNum;
	char*		BufPtr;
	plan_define_scada*	PlanDefPtr;
	int		HisTableNo, PlanId;
	short		HisFieldNo;
	float		PlanHisSamVal[96][SAMPLE_FIELD_NUM];
	TableOp		PlanToday(SCADA, PLAN_TODAY_NO);
	plan_today_scada	PlanTodayRec;

	BufSize = 8;
	BufPtr = (char* )malloc(BufSize);
	RecNum = GetAllRec(SCADA, PLAN_DEFINE_NO, BufPtr, BufSize);
	if(RecNum <= 0)  {
		free(BufPtr);
		return(-1);
	}

	PlanDefPtr = (plan_define_scada* )BufPtr;
	for(loop=0; loop<RecNum; loop++)  {

		PlanId = PlanDefPtr[loop].plan_id;
		HisTableNo = PlanDefPtr[loop].table_no;
		HisFieldNo = PlanDefPtr[loop].column_no;
		RetCode = PlanToday.TableGet((char* )&PlanId, (char* )&PlanTodayRec, 
						(int)sizeof(plan_today_scada));
		if(RetCode < 0)
			continue;

		for(loop2=0; loop2<96; loop2++)  
			PlanHisSamVal[loop2][HisFieldNo] = PlanTodayRec.val[loop2];
		
	}


	free(BufPtr);
	return(0);
}
*****/
